<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||	
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws. 
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.). 
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations). 
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Account/classes/PhpFox_ComponentFriendsRequest.class.php, $Author:  $ */



/** Display friends requests
 * 
 * Parameters:
 *  1. oUser (mandatory) - user account object
 * 
 * @package module.account
 * @version $Id: PhpFox_ComponentFriendsRequest.class.php 46 2007-12-14 23:12:21Z phpfoxsv $
 *   
 */
class PhpFox_Mod_Account_ComponentFriendsRequest extends BaseComponent
{

    /** Processing method
     */
	function process()
	{
	    $oAcc = $this->getParam('oUser');
	    assert(null != $oAcc);
	    
		$oSrvFriends = $this->_oModule->getService('Friends');
		/* @var $oSrvFriends PhpFox_Mod_Account_ServiceFriends */
		
		$oReq = &App::getRequest();
		$aProcessed = array();
		switch($oReq->getAction())
		{
		    case 'approve':
		        $mReqId = $oReq->get('id');
		        if ($mReqId)
		        {
		            $aProcessed =$oSrvFriends->approveRequest($mReqId, $oAcc);
		        }
		        else
		        {
		            //nothing to process
                    $this->addErrors(App::format('Mod_Account.error.no_items_selected'));
		        }
		        break;
		        
		    case 'deny':
		        $mReqId = $oReq->get('id');
		        if ($mReqId)
		        {
		            $aProcessed = $oSrvFriends->denyRequest($mReqId, $oAcc);
		        }
		        else
		        {
		            $this->addErrors(App::format('Mod_Account.error.no_items_selected'));
		        }
		        break;
		}
		
		$iPage = $oReq->getInt('page', 1);
		$iPageSize = App::getParam('public.pagesize');
		
		$oSrvAcc = &App::getModuleService('Account', 'Account');
		/* @var $oSrvAcc PhpFox_Mod_Account_ServiceAccount */
		
		list($aRequests, $iTotal) = $oSrvFriends->getRequestsList($oAcc, $iPage, $iPageSize);
        foreach($aRequests as $k=>$v)
        {
            $aRequests[$k]['age'] = $oSrvAcc->getAge($v);
        }


		$oPager = App::newObject('Pager', $iTotal, $iPage, $iPageSize);

		$oTpl = &$this->_oModule->getTemplate();
		$oTpl->assign(array(
			'aProcessed'   => $aProcessed,
			'sAction'      => $oReq->getAction(),
			'aRequests'    => $aRequests,
			'iNumRequests' => count($aRequests),
			'sUserPicUrl'  => App::getParam('user.pic.url'),
	        'aPaging'      => $oPager->getInfo(App::getUrl()),
		    'aErrors'      => $this->getErrors(),		
		));
		
		$srcHtml = $oTpl->fetch('FriendsRequest.html');
		return $srcHtml;
	}

}
?>